package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.McTimeInfoMapper;
import cc.rengu.igas.momp.common.entity.TMMcTimeInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jinan Liu
 * @date 2020/3/26 00:21
 * @description 营销活动机构规则数据库操作接口实现
 */
public class McTimeInfoMapperImpl implements McTimeInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 营销活动时间信息列表查询接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcTimeInfo>
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:04
     */
    @Override
    public List<TMMcTimeInfo> selectMcTimeInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcTimeInfoList");

        // 按接口赋查询条件
        TMMcTimeInfo mcTimeInfo = new TMMcTimeInfo();
        mcTimeInfo.setInstId(instId);
        mcTimeInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcTimeInfo, TMMcTimeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动时间信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcTimeInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 营销活动时间信息列表新增接口实现
     *
     * @param mcTimeInfoList 营销活动时间信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:04
     */
    @Override
    public int insertMcTimeInfoListByBank(List<TMMcTimeInfo> mcTimeInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMcTimeInfoListByBank");

        for (TMMcTimeInfo tmMcTimeInfo : mcTimeInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcTimeInfo, TMMcTimeInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>顺序号<{}>的营销活动时间规则信息失败!<{}>", tmMcTimeInfo.getInstId(), tmMcTimeInfo.getMcNo(), tmMcTimeInfo.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 营销活动时间信息列表修改接口实现
     *
     * @param mcTimeInfoList
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:04
     */
    @Override
    public int updateMcTimeInfoListByBank(List<TMMcTimeInfo> mcTimeInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcTimeInfoListByBank");

        for (TMMcTimeInfo tmMcTimeInfo : mcTimeInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcTimeInfo, TMMcTimeInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新法人机构号<{}>营销活动编号<{}>顺序号<{}>的营销活动时间规则信息失败!<{}>", tmMcTimeInfo.getInstId(), tmMcTimeInfo.getMcNo(), tmMcTimeInfo.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 营销活动时间信息列表删除接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:04
     */
    @Override
    public int deleteMcTimeInfoListByBank(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteMcTimeInfoListByBank");

        TMMcTimeInfo tmMcTimeInfo = new TMMcTimeInfo();
        tmMcTimeInfo.setInstId(instId);
        tmMcTimeInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcTimeInfo, TMMcTimeInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动时间规则信息失败!<{}>", tmMcTimeInfo.getInstId(), tmMcTimeInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增单条营销活动时间规则数据库操作接口实现
     *
     * @param instId     法人机构号
     * @param mcTimeInfo
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:05
     */
    @Override
    public int insertMcTimeInfo(String instId, TMMcTimeInfo mcTimeInfo) throws Exception {
        return 0;
    }

    /**
     * 更新单条营销活动时间规则数据库操作接口实现
     *
     * @param instId     法人机构号
     * @param mcTimeInfo
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:05
     */
    @Override
    public int updateMcTimeInfo(String instId, TMMcTimeInfo mcTimeInfo) throws Exception {
        return 0;
    }

    /**
     * 使用主键删除单条营销活动时间规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:04
     */
    @Override
    public int deleteMcTimeInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }


    /**
     * 使用主键查询单条营销活动时间规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:04
     */
    @Override
    public int selectMcTimeInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }
}
